/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/05/22
 * Program: SWTMonthlyAccountanceEntry.java
 */
package vn.smartware.co.client.report;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import vn.smartware.co.commons.Const;
import vn.smartware.co.commons.SWTUtils;
import vn.smartware.co.framework.bean.MenuDTO;
import vn.smartware.co.framework.bean.ProgramDTO;
import vn.smartware.co.framework.common.BaseConstant;
import vn.smartware.co.framework.exception.ClientException;
import vn.smartware.co.framework.exception.FatalException;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.exception.SessionTimeOutException;
import vn.smartware.co.framework.form.BaseForm;
import vn.smartware.co.framework.plugin.LangResources;
import vn.smartware.co.framework.swt.base.JNetComposite;
import vn.smartware.co.framework.utils.MsgBoxUtils;
import vn.smartware.co.java.net.commons.Utils;

/**
 * SWTMonthlyAccountanceEntry
 * @author thuctt
 */
@SuppressWarnings("rawtypes")
public class SWTMonthlyAccountanceEntry extends JNetComposite {

	private Text txtIndex;
	private static Log log = LogFactory.getLog(SWTMonthlyAccountanceEntry.class);
	
	private Button btnSave;

	private Button btnClear;

	private Combo cmbDspType;

	private Text txtVnName;

	private Text txtEnName;

	private Text txtClassID;


	private Table tblInfo;

	private Button btnSearch;

	private Text txtPrgIDEnd;

	private Text txtPrgIDStart;

	private Button btnEdit;

	private Button btnClose;

	private Button btnNew;

	private Button btnDelete;
	
	private Combo cmbMenu;

	private Button btnApply;
	
	private Group grp;
	
	private static final int TBL_ROW_COUNT = 16;
	
	private static final String ID = "ID";
	
	private String mode;
	
	private boolean enLang = true;
	
	private int edittingRow = -1;
	
	/**
	 * Constructor.
	 * @param parent 
	 * @param style 
	 * @param prgName 
	 */
	public SWTMonthlyAccountanceEntry(Composite parent, int style, String prgName) {

		super(parent, style, prgName);

		try {
//			setBackground(BaseConstant.COLOR_BGR_BASE);
//			
//			executeInit();
			
			if (!initData()){
				fatalErrorMsgBox();
				return;
			}
			
			clearAction();

		} catch (Exception e) {
			log.error("Exception.", e);
			fatalErrorMsgBox();
		}
	}
	
	/**
	 * init data to display
	 * @throws FatalException
	 */
	@SuppressWarnings("unchecked")
	private boolean initData() throws FatalException {

		try {
			Map map = new HashMap();
			map.put(Const.SYSTEM_CONFIG.PRG_DISPLAY_TYPE, Boolean.FALSE);
//			map = SWTUtils.ComboProcess.getComboInitItems(map, this);
			String[] items = (String[])map.get(Const.SYSTEM_CONFIG.PRG_DISPLAY_TYPE);
			if (items == null || items.length == 0 || Utils.isNullEmpty(items[1])){
				MsgBoxUtils.errorMsgBox("errors.COMBO_DATA_NOT_SET", 
						new Object[]{LangResources.getText("SWTBusinessProgramRegister.lblDisplay")});
				return false;
			}
			cmbDspType.setItems(items);
			
			BaseForm form = new BaseForm();
			form.setActionForm("baseForm");
			form.setActionMethod("getAvailableMenu");
			
			form = (BaseForm)execute(form);
			ArrayList menuLs = form.getMenu();
			if (menuLs.isEmpty()){
				MsgBoxUtils.errorMsgBox("errors.COMBO_DATA_NOT_SET", 
						new Object[]{LangResources.getText("SWTBusinessProgramRegister.lblMenu")});
				return false;
			}
			String[] menuArr = new String[menuLs.size()];
			String lang = LangResources.getCurrentLanguage();
			if (lang.equals(BaseConstant.EN_LANG_FILE)){
				enLang = true;
			}else{
				enLang = false;
			}
			for (int i = 0; i < menuLs.size(); i++) {
				MenuDTO menu = (MenuDTO)menuLs.get(i);
				String item = Integer.toString(menu.getMenuId());
				//TODO need to fix
//				if (enLang){
//					menuArr[i] = item + Const.COMBO_SEPERATE + menu.getEnName();
//				}else{
//					menuArr[i] = item + Const.COMBO_SEPERATE + menu.getVnName();
//				}
			}
			cmbMenu.setItems(menuArr);
		
		} catch (SessionTimeOutException e) {
			log.warn("SessionTimeOutException.", e);
		}
		return true;
	}

	/**
	 * init controls
	 */
	protected void initialize() {

		final CLabel lblID1 = new CLabel(this, SWT.CENTER | SWT.BORDER);
		lblID1.setBounds(30, 30, 100, 22);
		lblID1.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblID1.setText(LangResources.getText("SWTBusinessProgramRegister.lblID"));

		txtPrgIDStart = new Text(this, SWT.BORDER | SWT.RIGHT);
		txtPrgIDStart.setBounds(136, 30, 120, 22);
		txtPrgIDStart.setTextLimit(30);
		txtPrgIDStart.setTabs(1);
		txtPrgIDStart.setFont(BaseConstant.FONT_BASE_TEXT);
		txtPrgIDStart.setBackground(SWTResourceManager.getColor(255, 255, 255));

		final Label lbl1 = new Label(this, SWT.CENTER);
		lbl1.setText("-");
		lbl1.setBounds(288, 32, 22, 18);
		lbl1.setFont(BaseConstant.FONT_BASE_LABEL);
		lbl1.setBackground(BaseConstant.COLOR_BGR_BASE);

		txtPrgIDEnd = new Text(this, SWT.BORDER | SWT.RIGHT);
		txtPrgIDEnd.setBounds(343, 30, 120, 22);
		txtPrgIDEnd.setTextLimit(30);
		txtPrgIDEnd.setTabs(2);
		txtPrgIDEnd.setFont(BaseConstant.FONT_BASE_TEXT);
		txtPrgIDEnd.setBackground(SWTResourceManager.getColor(255, 255, 255));
		
		btnSearch = new Button(this, SWT.NONE);
		btnSearch.setText(LangResources.getText("btnSearch"));
		btnSearch.setBounds(697, 29, 100, 23);
		btnSearch.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnSearch.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnSearch.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				searchAction();
			}
		});
		
		final Label line1 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		line1.setBounds(0, 70, 812, 15);
		line1.setBackground(BaseConstant.COLOR_BGR_BASE);

		tblInfo = new Table(this, SWT.FULL_SELECTION | SWT.BORDER);
		tblInfo.addSelectionListener(new SelectionAdapter() {
			public void widgetDefaultSelected(final SelectionEvent arg0) {
				edittingRow = tblInfo.getSelectionIndex();
				TableItem item = tblInfo.getItem(edittingRow);
				Object data = item.getData();
				if (null == data){
					newAction();					
				}else{
					editAction();
				}
			}
		});
		tblInfo.setLinesVisible(true);
		tblInfo.setHeaderVisible(true);
		tblInfo.setBounds(15, 100, 782, 368);
		tblInfo.setFont(BaseConstant.FONT_BASE_LABEL);
		tblInfo.setData(BaseConstant.TABLE_ROW_COUNT, Integer.toString(TBL_ROW_COUNT));

		final TableColumn tcolStatus = new TableColumn(tblInfo, SWT.NONE);
		tcolStatus.setWidth(30);

		final TableColumn tcolID = new TableColumn(tblInfo, SWT.NONE);
		tcolID.setAlignment(SWT.RIGHT);
		tcolID.setWidth(50);
		tcolID.setText(LangResources.getText("SWTBusinessProgramRegister.tcolID"));
		
		final TableColumn tcolMenu = new TableColumn(tblInfo, SWT.NONE);
		tcolMenu.setWidth(150);
		tcolMenu.setText(LangResources.getText("SWTBusinessProgramRegister.tcolMenu"));
		
		final TableColumn tcolClassID = new TableColumn(tblInfo, SWT.NONE);
		tcolClassID.setWidth(250);
		tcolClassID.setText(LangResources.getText("SWTBusinessProgramRegister.tcolClassID"));
		
		final TableColumn tcolVnName = new TableColumn(tblInfo, SWT.NONE);
		tcolVnName.setWidth(200);
		tcolVnName.setText(LangResources.getText("SWTBusinessProgramRegister.tcolVnName"));
		
		final TableColumn tcolEnName = new TableColumn(tblInfo, SWT.NONE);
		tcolEnName.setWidth(200);
		tcolEnName.setText(LangResources.getText("SWTBusinessProgramRegister.tcolEnName"));
		
		final TableColumn tcolType = new TableColumn(tblInfo, SWT.NONE);
		tcolType.setWidth(80);
		tcolType.setText(LangResources.getText("SWTBusinessProgramRegister.tcolType"));
		
		btnDelete = new Button(this, SWT.NONE);
		btnDelete.setBounds(485, 480,100, 23);
		btnDelete.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnDelete.setText(LangResources.getText("btnDelete"));
		btnDelete.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnDelete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				deleteAction();
			}
		});

		btnNew = new Button(this, SWT.NONE);
		btnNew.setBounds(591, 480,100, 23);
		btnNew.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnNew.setText(LangResources.getText("btnNew"));
		btnNew.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnNew.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				newAction();
			}
		});

		btnEdit = new Button(this, SWT.NONE);
		btnEdit.setBounds(697, 480,100, 23);
		btnEdit.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnEdit.setText(LangResources.getText("btnEdit"));
		btnEdit.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnEdit.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				editAction();
			}
		});
		
		grp = new Group(this, SWT.NONE);
		grp.setSize(782, 123);
		grp.setLocation(15, 509);
		grp.setBackground(BaseConstant.COLOR_BGR_BASE);

		final CLabel lblID2 = new CLabel(grp, SWT.BORDER | SWT.CENTER);
		lblID2.setBounds(10, 25, 100, 22);
		lblID2.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblID2.setText(LangResources.getText("SWTBusinessProgramRegister.lblID"));

		txtIndex = new Text(grp, SWT.BORDER);
		txtIndex.setBounds(116, 25, 60, 22);
		txtIndex.setTextLimit(200);
		txtIndex.setFont(BaseConstant.FONT_BASE_TEXT);
		txtIndex.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);

		final CLabel lblDisplay = new CLabel(grp, SWT.BORDER | SWT.CENTER);
		lblDisplay.setBounds(182, 25, 70, 22);
		lblDisplay.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblDisplay.setText(LangResources.getText("SWTBusinessProgramRegister.lblDisplay"));

		cmbDspType = new Combo(grp, SWT.READ_ONLY);
		cmbDspType.setBounds(258, 25, 118, 24);
		cmbDspType.setTextLimit(200);
		cmbDspType.setFont(BaseConstant.FONT_BASE_TEXT);

		final CLabel lblClassID = new CLabel(grp, SWT.BORDER | SWT.CENTER);
		lblClassID.setBounds(406, 25, 100, 22);
		lblClassID.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblClassID.setText(LangResources.getText("SWTBusinessProgramRegister.lblClassID"));

		txtClassID = new Text(grp, SWT.BORDER);
		txtClassID.setBounds(512, 25, 260, 22);
		txtClassID.setTextLimit(100);
		txtClassID.setFont(BaseConstant.FONT_BASE_TEXT);
		txtClassID.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);

		final CLabel lblVnName = new CLabel(grp, SWT.BORDER | SWT.CENTER);
		lblVnName.setBounds(10, 55, 100, 22);
		lblVnName.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblVnName.setText(LangResources.getText("SWTBusinessProgramRegister.lblVnName"));

		txtVnName = new Text(grp, SWT.BORDER);
		txtVnName.setBounds(116, 55, 260, 22);
		txtVnName.setTextLimit(200);
		txtVnName.setFont(BaseConstant.FONT_BASE_TEXT);
		txtVnName.setBackground(SWTResourceManager.getColor(255, 255, 255));
//			public void mouseEnter(MouseEvent e) {
//				display.syncExec(new Runnable() {
//					public void run() {
//						lblMenu.setForeground(BaseConstant.COLOR_LINK_LABEL_MOUSE_ENTER);
//					}
//				});
//			}
//			public void mouseExit(MouseEvent arg0) {
//				display.syncExec(new Runnable() {
//					public void run() {
//						lblMenu.setForeground(BaseConstant.COLOR_LINK_LABEL);
//					}
//				});
//			}
//		});
//		lblMenu.addMouseListener(new MouseAdapter() {
//			public void mouseDown(MouseEvent arg0) {
//				log.info("Call Menu Display Screen");
//				openMenuRegisterScreen();
//			}
//		});
//		lblMenu.setForeground(BaseConstant.COLOR_LINK_LABEL);

		final CLabel lblEnName = new CLabel(grp, SWT.BORDER | SWT.CENTER);
		lblEnName.setBounds(406, 55, 100, 22);
		lblEnName.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblEnName.setText(LangResources.getText("SWTBusinessProgramRegister.lblEnName"));

		txtEnName = new Text(grp, SWT.BORDER);
		txtEnName.setBounds(512, 55, 260, 22);
		txtEnName.setTextLimit(200);
		txtEnName.setFont(BaseConstant.FONT_BASE_TEXT);
		txtEnName.setBackground(SWTResourceManager.getColor(255, 255, 255));

		final CLabel lblMenu = new CLabel(grp, SWT.CENTER | SWT.BORDER);
		lblMenu.setBounds(10, 84, 100, 22);
		lblMenu.setFont(BaseConstant.FONT_BASE_LABEL);
		lblMenu.setText(LangResources.getText("SWTBusinessProgramRegister.lblMenu"));

		cmbMenu = new Combo(grp, SWT.READ_ONLY);
		cmbMenu.setBounds(116, 85, 260, 24);
		cmbMenu.setTextLimit(200);
		cmbMenu.setFont(BaseConstant.FONT_BASE_LABEL);
		
		btnApply = new Button(grp, SWT.NONE);
		btnApply.setBounds(672, 85, 100, 23);
		btnApply.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnApply.setEnabled(false);
		btnApply.setText(LangResources.getText("btnApply"));
		btnApply.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnApply.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				applyAction();
			}
		});

		btnSave = new Button(this, SWT.NONE);
		btnSave.setBounds(485, 645, 100, 23);
		btnSave.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnSave.setText(LangResources.getText("btnSave"));
		btnSave.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnSave.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				saveAction();
			}
		});

		btnClear = new Button(this, SWT.NONE);
		btnClear.setBounds(591, 645, 100, 23);
		btnClear.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnClear.setText(LangResources.getText("btnClear"));
		btnClear.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnClear.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				clearAction();
			}
		});

		btnClose = new Button(this, SWT.NONE);
		btnClose.setBounds(697, 645, 100, 23);
		btnClose.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnClose.setText(LangResources.getText("btnClose"));
		btnClose.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnClose.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (!closeAction()){
					btnClose.setFocus();
				}
			}
		});
		
		this.getShell().setDefaultButton(btnSearch);
	}

	/**
	 * clearAction
	 */
	protected void clearAction() {
		
		SWTUtils.OperatorUI.clearAll(this.getChildren());
		
		tblInfo.setEnabled(false);
		btnDelete.setEnabled(false);
		btnNew.setEnabled(false);
		btnEdit.setEnabled(false);
		btnSave.setEnabled(false);
		grp.setEnabled(false);
		
		txtPrgIDEnd.setEnabled(true);
		txtPrgIDStart.setEnabled(true);
		btnSearch.setEnabled(true);
		this.mode = null;
		
		txtPrgIDStart.setFocus();
	}

	/**
	 * deleteAction
	 */
	protected void deleteAction() {
		
		try {
			TableItem item = tblInfo.getItem(tblInfo.getSelectionIndex());
			ProgramDTO prg = (ProgramDTO)item.getData();
			if (null == prg){
				MsgBoxUtils.errorMsgBox("errors.NOT_SELECTED_RECORD");
				tblInfo.setFocus();
				tblInfo.showSelection();
				return;
			}
			
			//
			ArrayList list = (ArrayList)tblInfo.getData();
			String status = item.getText(0);
			if (status.equals(Const.DELETE_SYMBOL)){
				MsgBoxUtils.warningMsgBox("warning.RECORD_DELETED");
				tblInfo.setFocus();
				tblInfo.showSelection();
				return;
			}
			
			//
			if (MsgBoxUtils.questionMsgBox("question.BE_SURE") == SWT.NO){
				btnDelete.setFocus();
				return;
			}
			
			//remove item
			if (Utils.isNullEmpty(status) || status.equals(Const.UPDATE_SYMBOL)){
				String id = (String)item.getData(ID);
				if (!id.equals("-1")){
					item.setText(0, Const.DELETE_SYMBOL);
					item.setBackground(Const.COLOR_DATA_ROW_CHANGED);
					btnSave.setEnabled(true);
					return;
				}
			}
			list.remove(tblInfo.getSelectionIndex());
			tblInfo.remove(tblInfo.getSelectionIndex());
			btnSave.setEnabled(true);
			
		}catch (ClientException e) {
			log.error("deleteAction ClientException", e);
		}
	}

	/**
	 * displayAction
	 */
	protected void searchAction() {
		
		try {
			// check action
			if (!Utils.isNullEmpty(txtPrgIDStart.getText())
					&& !Utils.isNumeric(txtPrgIDStart.getText().trim())){
				MsgBoxUtils.errorMsgBox("errors.NUMERIC_FORMAT", new Object[]{"ID"});
				txtPrgIDStart.setFocus();
				return;
			}
			if (!Utils.isNullEmpty(txtPrgIDEnd.getText())
					&& !Utils.isNumeric(txtPrgIDEnd.getText().trim())){
				MsgBoxUtils.errorMsgBox("errors.NUMERIC_FORMAT", new Object[]{"ID"});
				txtPrgIDEnd.setFocus();
				return;
			}
			//
			BaseForm form = new BaseForm();
			form.setActionForm("baseForm");
			form.setActionMethod("getPrograms");
			
			form.setIdStart(Utils.toEmpty(txtPrgIDStart.getText()));
			form.setIdEnd(Utils.toEmpty(txtPrgIDEnd.getText()));
			
			form = (BaseForm)execute(form);
			
			if (form.getPrgList().isEmpty()){
				MsgBoxUtils.warningMsgBox("warning.NO_DATA_FOUND");
				return;
			}
			
			tblInfo.setEnabled(true);
			btnDelete.setEnabled(true);
			btnNew.setEnabled(true);
			btnEdit.setEnabled(true);
			
			btnSave.setEnabled(false);
			grp.setEnabled(false);
			txtPrgIDStart.setEnabled(false);
			txtPrgIDEnd.setEnabled(false);
			btnSearch.setEnabled(false);
			btnApply.setEnabled(false);
			
			displayTablePrograms(form.getPrgList());
			
			tblInfo.setFocus();
			
		} catch (SessionTimeOutException e) {
			log.error("SWTBusinessProgramRegister displayAction SessionTimeOutException", e);
		} catch (ServerException e) {
			log.error("SWTBusinessProgramRegister displayAction ServerException.", e);
			fatalErrorMsgBox();
		} catch (FatalException e) {
			log.error("SWTBusinessProgramRegister displayAction FatalException.", e);
			fatalErrorMsgBox();
		}
	}

	/**
	 * display table program
	 * @param list
	 */
	private void displayTablePrograms(ArrayList list) {

		tblInfo.removeAll();
		
		int rowcount = TBL_ROW_COUNT;
		if (list.size() >= rowcount){
			rowcount = list.size() + 2;
		}
		for (int i = 0; i < rowcount; i++) {
			TableItem tblItem = new TableItem(tblInfo, SWT.NONE);
			
			if (i < list.size()){
				ProgramDTO prg = (ProgramDTO)list.get(i);
				
				String[] items = new String[tblInfo.getColumnCount()];
				int idx = 0;
				items[idx++] = "";
				items[idx++] = prg.getIndex();
				items[idx++] = SWTUtils.ComboProcess.getLabel(cmbMenu, Integer.toString(prg.getMenuId()));
				items[idx++] = prg.getPrgClass();
				items[idx++] = prg.getVnName();
				items[idx++] = prg.getEnName();
				items[idx++] = prg.getDspDivisionName();
				
				tblItem.setText(items);
				tblItem.setData(prg);
				tblItem.setData(ID, Integer.toString(prg.getPrgId()));
			}
			
			if (i%2 == 0){
				tblItem.setBackground(BaseConstant.COLOR_TABLE_ROW_EVEN);
			}
		}
		tblInfo.setData(list);
		tblInfo.setSelection(0);
	}

	/**
	 * editAction
	 */
	protected void editAction() {
		
		this.mode = Const.MODE_EDIT;
		
		//clear text detail area
		SWTUtils.OperatorUI.clearAll(grp.getChildren());
		
		//set data edit
		TableItem item = tblInfo.getItem(edittingRow);
		ProgramDTO prg = (ProgramDTO)item.getData();
		if (null == prg){
			try {
				MsgBoxUtils.errorMsgBox("errors.NOT_SELECTED_RECORD");
				return;
				
			} catch (ClientException e) {
				log.error("editAction ClientException.", e);
				fatalErrorMsgBox();
			}
		}
		//set data row to detail group
		if (prg.getPrgId() != -1){
			txtIndex.setData(Integer.toString(prg.getPrgId()));
		}
		txtIndex.setText(prg.getIndex());
		txtClassID.setText(prg.getPrgClass());
		txtEnName.setText(prg.getEnName());
		txtVnName.setText(prg.getVnName());
		SWTUtils.ComboProcess.setDisplayLabel(cmbDspType, prg.getDspDivision());
		SWTUtils.ComboProcess.setDisplayLabel(cmbMenu, Integer.toString(prg.getMenuId()));
		
		//init control
		grp.setEnabled(true);
		txtIndex.setFocus();
		btnSave.setEnabled(true);
		btnApply.setEnabled(true);
	}

	/**
	 * newAction
	 */
	protected void newAction() {
		
		this.mode = Const.MODE_NEW;
		
		//clear text detail area
		SWTUtils.OperatorUI.clearAll(grp.getChildren());
		grp.setEnabled(true);
		txtIndex.setFocus();
		btnSave.setEnabled(true);
		btnApply.setEnabled(true);
	}

	/**
	 * saveAction
	 */
	@SuppressWarnings("unchecked")
	protected void saveAction() {
		
		try {
			ArrayList list = new ArrayList();
			TableItem[] rows = tblInfo.getItems();
			for (int i = 0; i < rows.length; i++) {
				String statusText = rows[i].getText(0);
				if (!Utils.isNullEmpty(statusText)){
					String status = null;
					if (statusText.equals(Const.INSERT_SYMBOL)){
						status = Const.MODE_NEW;
					}else if (statusText.equals(Const.UPDATE_SYMBOL)){
						status = Const.MODE_EDIT;
					}else if (statusText.equals(Const.DELETE_SYMBOL)){
						status = Const.MODE_DEL;
					}
					
					ProgramDTO prg = (ProgramDTO)rows[i].getData();
					prg.setMode(status);
					list.add(prg);
				}
			}
			
			if (list.isEmpty()){
				MsgBoxUtils.warningMsgBox("warning.NO_DATA_SAVE");
				return;
			}
			
			//lock screen
			lockScreen(true);
			
			BaseForm form = new BaseForm();
			form.setPrgList(list);
			
			form.setActionForm("baseForm");
			form.setActionMethod("saveAction");
			form = (BaseForm)execute(form);
			
			MsgBoxUtils.infoMsgBox("info.SAVE_SUCCESSFUL");
			
			//unlock screen
			lockScreen(false);
			
			clearAction();
			searchAction();
		
		} catch (SessionTimeOutException e) {
			log.error("saveAction SessionTimeOutException.", e);
			
		} catch (FatalException e) {
			log.error("saveAction FatalException.", e);
			fatalErrorMsgBox();
		}
	}
	
	/**
	 * apply action
	 *
	 */
	@SuppressWarnings("unchecked")
	protected void applyAction() {

		if (!checkData()){
			return;
		}
		
		ProgramDTO prg = new ProgramDTO();
		prg.setIndex(Utils.toEmpty(txtIndex.getText()));
		prg.setPrgClass(Utils.toEmpty(txtClassID.getText()));
		prg.setDspDivision(SWTUtils.ComboProcess.getSelectingValue(cmbDspType));
		prg.setDspDivisionName(SWTUtils.ComboProcess.getSelectingLabel(cmbDspType));
		prg.setEnName(Utils.toEmpty(txtEnName.getText()));
		prg.setVnName(Utils.toEmpty(txtVnName.getText()));
		prg.setMenuId(Integer.parseInt(SWTUtils.ComboProcess.getSelectingValue(cmbMenu)));
		
		if (this.mode == Const.MODE_EDIT && !Utils.isNullEmpty(txtIndex.getData())){
			prg.setPrgId(Integer.parseInt(txtIndex.getData().toString()));
		}
		
		String[] items = new String[tblInfo.getColumnCount()];
		int idx = 1;
		items[idx++] = prg.getIndex();
		items[idx++] = SWTUtils.ComboProcess.getSelectingLabel(cmbMenu);
		items[idx++] = prg.getPrgClass();
		items[idx++] = prg.getVnName();
		items[idx++] = prg.getEnName();
		items[idx++] = prg.getDspDivisionName();
		
		ArrayList list = (ArrayList)tblInfo.getData();
		TableItem tblItem = null;
		if (this.mode == Const.MODE_EDIT){
			if (prg.getPrgId() != -1){
				items[0] = Const.UPDATE_SYMBOL;
			}
			tblItem = tblInfo.getItem(edittingRow);
			tblItem.setText(items);
			tblItem.setData(prg);
			tblItem.setData(ID, Integer.toString(prg.getPrgId()));
			list.set(edittingRow, prg);
			
		}else{
			if (list.size() >= tblInfo.getItemCount()){
				tblItem = new TableItem(tblInfo, SWT.NONE);
			}else{
				tblItem = tblInfo.getItem(list.size());
			}
			items[0] = Const.INSERT_SYMBOL;
			tblItem.setText(items);
			tblItem.setData(prg);
			tblItem.setData(ID, Integer.toString(prg.getPrgId()));
			tblItem.setData(prg);
			list.add(prg);
		}
		tblInfo.setData(list);
		this.mode = null;
		this.edittingRow = -1;
		
		SWTUtils.OperatorUI.clearAll(grp.getChildren());
		tblItem.setBackground(Const.COLOR_DATA_ROW_CHANGED);
		grp.setEnabled(false);
		btnApply.setEnabled(false);
		tblInfo.showSelection();
		tblInfo.setFocus();
	}

	/**
	 * check data input
	 * @return boolean
	 */
	private boolean checkData() {
		
//		try {
//			if (!SWTUtils.Check.checkRequired(txtIndex.getText(), LangResources.getText("SWTBusinessProgramRegister.lblID"))){
//				txtIndex.setFocus();
//				return false;
//			}
//			
//			if (!SWTUtils.Check.checkRequired(txtClassID.getText(), LangResources.getText("SWTBusinessProgramRegister.lblClassID"))){
//				txtClassID.setFocus();
//				return false;
//			}
//			
//			if (!SWTUtils.Check.checkRequired(txtEnName.getText(), LangResources.getText("SWTBusinessProgramRegister.lblEnName"))){
//				txtEnName.setFocus();
//				return false;
//			}
//			
//			if (!SWTUtils.Check.checkRequired(txtVnName.getText(), LangResources.getText("SWTBusinessProgramRegister.lblVnName"))){
//				txtVnName.setFocus();
//				return false;
//			}
//			
//		} catch (ClientException e) {
//			log.error("checkData ClientException.", e);
//			fatalErrorMsgBox();
//			return false;
//		}
		return true;
	}

	/* (non-Javadoc)
	 * @see vn.smartware.co.framework.swt.base.JNetComposite#lockScreen(boolean)
	 */
	protected void lockScreen(boolean flg) {
		
		SWTUtils.OperatorUI.setEnableAll(this.getChildren(), !flg);
	}

	@Override
	protected boolean loadData() throws FatalException {
		// TODO Auto-generated method stub
		return false;
	}
}